package org.example.testone.service.impl;

import com.mysql.cj.util.StringUtils;
import org.example.testone.common.Result;
import org.example.testone.constant.HttpStatusCode;
import org.example.testone.entity.DailyStats;
import org.example.testone.entity.WorkOrder;
import org.example.testone.mapper.WorkOrderMapper;
import org.example.testone.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkOrderServiceImpl implements WorkOrderService {

    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Override
    public Result save(WorkOrder workOrder) {
        Result result = nullVerify(workOrder);
        // 校验失败，直接返回
        if(result.getStatus().equals(HttpStatusCode.SERVER_ERROR.getCode())) {
            return result;
        }
        workOrderMapper.insertWorkOrder(workOrder);
        result.setMessage("添加成功");
        return result;
    }

    @Override
    public Result update(WorkOrder workOrder) {
        Result result = nullVerify(workOrder);
        // 校验失败，直接返回
        if(result.getStatus().equals(HttpStatusCode.SERVER_ERROR.getCode())) {
            return result;
        }
        workOrderMapper.updateWorkOrder(workOrder);
        result.setMessage("修改成功");
        return result;
    }

    @Override
    public Result delete(int id) {
        workOrderMapper.deleteWorkOrder(id);
        return new Result(200, "删除成功");
    }

    @Override
    public Result search(int offset, int pageSize) {
        List<WorkOrder> allWorkOrders = workOrderMapper.getAllWorkOrders(offset, pageSize);
        return new Result(200, "查询成功", allWorkOrders);
    }

    @Override
    public Result fenpai(int order_id, int dept_id) {
        int isHaveDept = workOrderMapper.getDeptId(String.valueOf(dept_id));
        Result res = new Result(200, "");
        // 校验部门
        if(isHaveDept == 0) {
            res.setStatus(500);
            res.setMessage("部门不存在");
            return res;
        }

        // 维护分派时间
        String fenpaiTime = LocalDate.now().toString();
        WorkOrder wordOrder = workOrderMapper.getWorkOrderById(order_id);
        workOrderMapper.updateWorkOrder(wordOrder);
        String format = DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalTime.now());
        fenpaiTime = fenpaiTime +  " " + format;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            java.util.Date utilDate = formatter.parse(fenpaiTime);
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            wordOrder.setFenpaiTime(sqlDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        // 查询7月每天的工单总量、超期率
        List<DailyStats> totalByDay = workOrderMapper.getTotalByDay();
        // 查询7月每个部门的工单总量、超期率
        List<DailyStats> totalByDept = workOrderMapper.getTotalByDept();

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("totalByDay", totalByDay);
        resMap.put("totalByDept", totalByDept);

        return res.setData(resMap);
    }

    /**
     * 空校验
     */
    public Result nullVerify(WorkOrder workOrder) {
        Result result = new Result(200, "");
        String orderNo = workOrder.getOrderNo();
        if(StringUtils.isNullOrEmpty(workOrder.getOrderNo())) {
            result.setStatus(500);
            result.setMessage("工单编号不能为空");
        }
        if(StringUtils.isNullOrEmpty(workOrder.getOrderType())) {
            result.setStatus(500);
            result.setMessage("工单类型不能为空");
        }
        if(StringUtils.isNullOrEmpty(workOrder.getHandleDeptId())) {
            result.setStatus(500);
            result.setMessage("部门编号不能为空");
        }
        if(StringUtils.isNullOrEmpty(workOrder.getTitle())) {
            result.setStatus(500);
            result.setMessage("编号不能为空");
        }
        if(StringUtils.isNullOrEmpty(workOrder.getContent())) {
            result.setStatus(500);
            result.setMessage("内容不能为空");
        }
        if(workOrder.getCreateTime() != null && StringUtils.isNullOrEmpty(workOrder.getCreateTime().toString())) {
            result.setStatus(500);
            result.setMessage("创建时间不能为空");
        }

        // 工单编号校验
        int isHaveOrder = workOrderMapper.getOrderNo(orderNo);
        if(isHaveOrder > 0) {
            result.setStatus(500);
            result.setMessage("工单编号已经存在");
        }

        return result;
    }
}
